/*    

    bobbietalk - a visual programming environment for toy robotics

    Copyright (C) 2013  Roberto Tiella

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
package bobbietalk.semantics.compiler;

import bobbietalk.gui.ProgrammingBlock;
import bobbietalk.gui.blocks.parametrized.ParametrizedBlock;
import java.io.StringWriter;
import java.util.List;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

/**
 *
 * @author tiella
 */
public class PseudoCodeCompiler extends AbstractCompiler {

    public PseudoCodeCompiler(String tmpDirPath) {
        super(tmpDirPath);
    }

    @Override
    protected boolean generateCode(BlockGraph g, StringBuilder sb, List<String> messages, boolean isSubroutine) {

        ProgrammingBlock b = g.getBeginBlock();
        Velocity.init();

        while (b != null) {

            if (b instanceof ParametrizedBlock) {

                ParametrizedBlock pb = (ParametrizedBlock) b;

                String template = pb.getCodeTemplate("pseudo");


                VelocityContext context = new VelocityContext();
                context.put("block", pb);
                StringWriter sw = new StringWriter();

                Velocity.evaluate(context, sw, "", template);

                sb.append(sw.toString());
                sb.append('\n');

            }

            b = b.getNextBlock();

        }

        return true;
    }

    @Override
    protected boolean compileWithExternalTool(String ppath, List<String> messages) {
        return true;
    }

    @Override
    public String getExension() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
